package com.zr.inquiry.total.service.impl;

import com.zr.inquiry.partner.mapper.PartnerMapper;
import com.zr.inquiry.partner.model.pojo.Partner;
import com.zr.inquiry.spare.mapper.SpareMapper;
import com.zr.inquiry.spare.model.pojo.InquirySpareDetailsVo;
import com.zr.inquiry.total.mapper.InquiryMapper;
import com.zr.inquiry.total.model.pojo.*;
import com.zr.inquiry.total.service.InquiryService;
import com.zr.inquiry.valid.InquiryEditValid;
import com.zr.utils.page.PageUtil;
import com.zr.utils.result.ResultBuilderUtil;
import com.zr.utils.result.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @ClassName InquiryServiceImpl
 * @Description TODO 用于实现询价信息的增删改查
 * @Author  马家奎
 * @Date 2019/11/14 19:57
 */
@Service
public class InquiryServiceImpl implements InquiryService {
    @Autowired
    private InquiryEditValid valid;
    @Autowired
    private PartnerMapper partnerMapper;
    @Autowired
    private SpareMapper spareMapper;
    @Autowired
    private InquiryMapper inquiryMapper;
    @Autowired
    private PageUtil<QueryPageInquiryResultVo> page;
    /*
    * @Author 马家奎
    * @Description //TODO 分页查询询价清单 * @Date
    * @Param
    * @return
    **/
    @Override
    public PageUtil<QueryPageInquiryResultVo> queryPage(QueryPageInquiryVo pageInquiryVo) {

        page.setPageIndex(pageInquiryVo.getPageIndex());
        page.setTotalNumber(inquiryMapper.getTotalCount(pageInquiryVo));
        page.setPageSize(pageInquiryVo.getPageSize());

        System.out.println(pageInquiryVo);
        pageInquiryVo.setIndex(page.getIndex());
        page.setDataList(inquiryMapper.getCurrentPageData(pageInquiryVo));
        return page;
    }

    /*
    * @Author 马家奎
    * @Description //TODO 添加询价清单 * @Date
    * @Param
    * @return
    **/
    @Transactional
    @Override
    public ResultUtil add(InquiryAddVo addVo) {
        //法人、询价类型、询价币别是否存在
        Integer currencyCode = addVo.getCurrencyCode();
        Integer type = addVo.getType();
        Integer legalPersonId = addVo.getLegalPersonId();
        ResultUtil othersIsExist = valid.validateLegalPersonExist(currencyCode, type, legalPersonId);
        if (!othersIsExist.getSuccess()) {
            return othersIsExist;
        }
        //开始日期是否在结束日期之前
        ResultUtil isBefore = valid.validateTimeIsBefore(addVo.getStartTime(),addVo.getEndTime());
        if (!isBefore.getSuccess()) {
            return isBefore;
        }
        //验证备件不能重复和必须存在
        ResultUtil spareIsRepeatedOrIsExist = valid.validateSpareIsRepeatedAndIsExist(addVo.getInquiryDetailsList());
        if (!spareIsRepeatedOrIsExist.getSuccess()) {
            return spareIsRepeatedOrIsExist;
        }
        //验证备件一定有生产他的供应商
        ResultUtil spareIsExistPartner = valid.validateSpareIsExistPartner(addVo.getSupplierDetailsList(),addVo.getInquiryDetailsList());
        if (!spareIsExistPartner.getSuccess()) {
            return spareIsExistPartner;
        }
        //验证供应商不能重复和必须存在
        ResultUtil partnerIsRepeatedAndIsExist = valid.validatePartnerIsRepeatedAndIsExist(addVo.getSupplierDetailsList());
        if (!partnerIsRepeatedAndIsExist.getSuccess()) {
            return partnerIsRepeatedAndIsExist;
        }
        //验证供应商一定有可以生产的备件
        ResultUtil partnerIsExistSpare = valid.validatePartnerIsExistSpare(addVo.getSupplierDetailsList(),addVo.getInquiryDetailsList());
        if (!partnerIsExistSpare.getSuccess()) {
            return partnerIsExistSpare;
        }
        //入库
        //1.表头数据入库
        addVo.setRequestForQuotationId(UUID.randomUUID().toString());
        Date startTime = new Date();
        addVo.setCreateTime(startTime);
        addVo.setUpdateTime(startTime);
        addVo.setUpdateName("马家奎");
        addVo.setCreatorName("马家奎");
        inquiryMapper.addInquiryTotal(addVo);
        //2.询价供应商入库
        addVo.getSupplierDetailsList().forEach(partner -> partner.setInquiryId(addVo.getId()));
        partnerMapper.addPartner(addVo.getSupplierDetailsList());
        //3.询价备件数据入库
        addVo.getInquiryDetailsList().forEach(inquirySpare -> inquirySpare.setInquiryId(addVo.getId()));
        spareMapper.addSpare(addVo.getInquiryDetailsList());

        return ResultBuilderUtil.success(null);
    }
    /*
    * @Author 马家奎
    * @Description //TODO 修改询价清单 * @Date
    * @Param
    * @return
    **/
    @Transactional
    @Override
    public ResultUtil update(InquiryUpdateVo updateVo) {
        //清除询价备件数据
        Integer inquiryId = spareMapper.queryByQuotationId(updateVo.getRequestForQuotationId());
        partnerMapper.deletePartnerByInquiryId(inquiryId);
        //清除供应商数据
        spareMapper.deleteSpareByInquiryId(inquiryId);
        //法人、询价类型、询价币别是否存在
        Integer currencyCode = updateVo.getInquiryDetailsList().get(0).getCurrencyCode();
        Integer type = updateVo.getType();
        Integer legalPersonId = updateVo.getLegalPersonId();
        ResultUtil othersIsExist = valid.validateLegalPersonExist(currencyCode, type, legalPersonId);
        if (!othersIsExist.getSuccess()) {
            return othersIsExist;
        }
        //开始日期是否在结束日期之前
        ResultUtil isBefore = valid.validateTimeIsBefore(updateVo.getStartTime(),updateVo.getEndTime());
        if (!isBefore.getSuccess()) {
            return isBefore;
        }
        //验证备件必须存在且不能重复
        ResultUtil spareIsExist = valid.validateSpareIsRepeatedAndIsExist(updateVo.getInquiryDetailsList());
        if (!spareIsExist.getSuccess()) {
            return spareIsExist;
        }
        //验证备件一定有生产他的供应商
        ResultUtil spareIsExistPartner = valid.validateSpareIsExistPartner(updateVo.getSupplierDetailsList(),updateVo.getInquiryDetailsList());
        if (!spareIsExistPartner.getSuccess()) {
            return spareIsExistPartner;
        }
        //验证供应商不能重复并且必须存在
        ResultUtil partnerIsRepeated = valid.validatePartnerIsRepeatedAndIsExist(updateVo.getSupplierDetailsList());
        if (!partnerIsRepeated.getSuccess()) {
            return partnerIsRepeated;
        }
        //验证供应商一定有可以生产的备件
        ResultUtil partnerIsExistSpare = valid.validatePartnerIsExistSpare(updateVo.getSupplierDetailsList(),updateVo.getInquiryDetailsList());
        if (!partnerIsExistSpare.getSuccess()) {
            return partnerIsExistSpare;
        }
        //入库

        //1.表头数据入库
        Date startTime = new Date();
        updateVo.setUpdateTime(startTime);
        updateVo.setUpdateName("马家奎");
        inquiryMapper.updateInquiryTotalByQuotationId(updateVo);
        //2.询价供应商入库
        //新数据入库
        List<Partner> suppliers= updateVo.getSupplierDetailsList();
        for (Partner partner : suppliers) {
            partner.setInquiryId(inquiryId);
        }
        partnerMapper.addPartner(updateVo.getSupplierDetailsList());
        //3.询价备件数据入库
        //新数据入库
        List<InquirySpareDetailsVo> spares= updateVo.getInquiryDetailsList();
        for (InquirySpareDetailsVo spare : spares) {
            spare.setInquiryId(inquiryId);
        }
        spareMapper.addSpare(spares);
       return ResultBuilderUtil.success(null);
    }
    @Transactional
    @Override
    public ResultUtil modifyState(ModifyStateVo modifyStateVo) {

        inquiryMapper.modifyState(modifyStateVo);

        return  ResultBuilderUtil.success(null);
    }
}
